package com.fh.shop.api.cart.controller;

import com.alibaba.fastjson.JSON;
import com.fh.shop.api.annotation.NeedLogin;
import com.fh.shop.api.cart.biz.ICartService;
import com.fh.shop.api.cart.vo.CartVo;
import com.fh.shop.api.common.Constant;
import com.fh.shop.api.common.ServerResponse;
import com.fh.shop.api.member.vo.MemberVo;
import com.fh.shop.api.util.KeyUtil;
import com.fh.shop.api.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/carts")
@Api(tags = "购物车接口")
public class CartController {
    @Resource
    private HttpServletRequest request;
    @Resource
    private ICartService cartService;


    @PostMapping
    @NeedLogin
    @ApiOperation("添加购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId",value = "商品id",required = true,example = "0"),
            @ApiImplicitParam(name = "count",value = "商品数量",required = true,example = "0"),
            @ApiImplicitParam(name = "x-auth",value = "请求头",required = true,paramType = "header")
    })
    public ServerResponse addCart(Long productId,Long count){
        MemberVo memberVO = (MemberVo) request.getAttribute(Constant.MEMBERVO);
        Long memberId = memberVO.getId();
        return cartService.addCart(productId,count,memberId);
    }

    @GetMapping
    @NeedLogin
    @ApiOperation("查询购物车")
    @ApiImplicitParam(name = "x-auth",value = "请求头",required = true,paramType = "header")
    public ServerResponse list(){
        MemberVo memberVO = (MemberVo) request.getAttribute(Constant.MEMBERVO);
        Long memberId = memberVO.getId();
        String key = KeyUtil.CartKey(memberId);
        String cartVoJson = RedisUtil.hget(key,Constant.MEMBER_CART);
        CartVo cartVo = JSON.parseObject(cartVoJson, CartVo.class);
        return ServerResponse.success(cartVo);
    }

    @DeleteMapping("{id}")
    @NeedLogin
    @ApiOperation("删除购物车商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "商品id",required = true,example = "0"),
            @ApiImplicitParam(name = "x-auth",value = "请求头",required = true,paramType = "header")
    })
    public ServerResponse delete(@PathVariable Long id){
        MemberVo memberVo = (MemberVo) request.getAttribute(Constant.MEMBERVO);
        Long memberId = memberVo.getId();
        return cartService.deleteCart(id,memberId);
    }

    @DeleteMapping
    @NeedLogin
    @ApiOperation("批量删除购物车商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "商品ids,如1,2,3...",required = true),
            @ApiImplicitParam(name = "x-auth",value = "请求头",required = true,paramType = "header")
    })
    public ServerResponse deleteBatch(String ids){
        MemberVo memberVo = (MemberVo) request.getAttribute(Constant.MEMBERVO);
        Long memberId = memberVo.getId();
        return cartService.deleteBatch(ids,memberId);
    }

    @GetMapping("/count")
    @NeedLogin
    @ApiOperation("查询购物车条数")
    @ApiImplicitParam(name = "x-auth",value = "请求头",required = true,paramType = "header")
    public ServerResponse queryCartCount(){
        MemberVo memberVo = (MemberVo) request.getAttribute(Constant.MEMBERVO);
        Long memberId = memberVo.getId();
        return cartService.queryCartCount(memberId);
    }
}
